package com.credithc.signature.manager.cron;

import com.credithc.signature.common.mapper.ChannelMapper;
import com.credithc.signature.common.mapper.DatapushMapper;
import com.credithc.signature.common.po.Channel;
import com.credithc.signature.common.po.DatapushChannel;
import com.credithc.signature.common.po.DatapushTemplate;
import com.credithc.signature.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @desc: 数据推送定时任务<br />
 * @author: Jingbo Hu<br/>
 * @time：2020年05⽉19⽇<br/>
 * @version：v1.0.0<br>
 */
@Slf4j
@Component
@Async
public class DataPushCron {

    @Autowired
    DatapushMapper datapushMapper;

    @Autowired
    ChannelMapper channelMapper;


    /**
     * 每天凌晨3点执行一次
     */
    @Scheduled(cron = "0 0 3 * * ?")
    public void dataPushCron() {
        log.info("执行定时任务[统计各业务系统签约量]每天凌晨3点执行一次,现在时间为：" + DateUtil.getDefaultDateTimeStr());
        try {
            // 1.首先查看数据库表中是否有数据
            int count = datapushMapper.checkDataTableIsEmpty();
            if (count == 0) {
                // 2.重新统计所有数据插入数据库
                List<DatapushChannel> dataPushInfos = datapushMapper.listStatisticsInfos();
                buildChannelName(dataPushInfos);
                datapushMapper.insertDataList(dataPushInfos);
            } else {
                // 3.查询昨天的数据，并更新到表中
                String date = DateUtil.getYesterday();
                List<DatapushChannel> dataPushInfos = datapushMapper.listYesterdayData();
                buildChannelName(dataPushInfos);
                datapushMapper.deleteByDate(date);
                datapushMapper.updateYesterdayData(dataPushInfos);
            }
        } catch (Exception e) {
            log.error("执行定时任务[统计各业务系统签约量]失败...", e);
        }
    }

    /**
     * 每天凌晨3点10分执行一次
     */
    @Scheduled(cron = "0 10 3 * * ?")
    public void dataPushCron2() {
        log.info("执行定时任务[统计各模板签约量]每天凌晨3点10分执行一次,现在时间为：" + DateUtil.getDefaultDateTimeStr());
        try {
            // 1.首先查看数据库表中是否有数据
            int count = datapushMapper.checkDataTableIsEmpty2();
            if (count == 0) {
                // 2.重新统计所有数据插入数据库
                List<DatapushTemplate> dataPushInfos = datapushMapper.listStatisticsInfos2();
                datapushMapper.insertDataList2(dataPushInfos);
            } else {
                // 3.查询昨天的数据，并更新到表中
                String date = DateUtil.getYesterday();
                List<DatapushTemplate> dataPushInfos = datapushMapper.listYesterdayData2();
                datapushMapper.deleteByDate2(date);
                datapushMapper.updateYesterdayData2(dataPushInfos);
            }
        } catch (Exception e) {
            log.error("执行定时任务[统计各模板签约量]失败...", e);
        }
    }

    /**
     * 封装渠道名称
     *
     * @param dataPushInfos
     */
    private void buildChannelName(List<DatapushChannel> dataPushInfos) {
        for (DatapushChannel dataPushInfo : dataPushInfos) {
            Channel channel = channelMapper.selectByChannelNo(dataPushInfo.getChannelNo());
            dataPushInfo.setChannelName(channel.getChannelName());
        }
    }
}
